<!-- 新增指标弹窗 -->
    <form id="addIdenManageForm" lay-filter="addGroupForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">指标名称</label>
            <div class="layui-input-block">
                <input name="iden_name" placeholder="" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">指标代码别名</label>
            <div class="layui-input-block">
                <input name="iden_alias_code" placeholder="" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">指标数据类型</label>
            <div class="layui-input-block" id="addIdenTypeBox">

            </div>
        </div>
        <div class="layui-form-item iden_length">
            <label class="layui-form-label">指标长度</label>
            <div class="layui-input-block" id="iden_length">
                <input name="iden_length" placeholder="" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" />
            </div>
        </div>
        <div class="layui-form-item iden_precision">
            <label class="layui-form-label">指标精度</label>
            <div class="layui-input-block">
                <input name="iden_precision" placeholder="" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">指标简称</label>
            <div class="layui-input-block">
                <input name="iden_simple_name" placeholder="" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">获取渠道</label>
            <div class="layui-input-block" id="procurementTypeBox">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">使用方式</label>
            <div class="layui-input-block" id="useTypeBox">
            </div>
        </div>
        <div class="layui-form-item identGroup">
            <label class="layui-form-label">枚举分组</label>
            <div class="layui-input-block" >
                <select name="identid" placeholder="请选择" id="identGroupBOX" lay-filter="identGroup">

                </select>
            </div>
        </div>
        <div class="layui-form-item ident_version">
            <label class="layui-form-label">枚举分组版本</label>
            <div class="layui-input-block">
                <select name="ident_version" placeholder="请选择请求方法" id="ident_version_box" >

                </select>
            </div>
        </div>
        <div class="layui-form-item idenUnitsGroup">
            <label class="layui-form-label ">指标单位分组</label>
            <div class="layui-input-block" >
                <select name="iden_unit_name" placeholder="" id="idenUnitsGroupBOX" lay-filter="idenUnitsGroupBOX">

                </select>
            </div>
        </div>
        <div class="layui-form-item idenUnitsValue">
            <label class="layui-form-label ">指标单位名称</label>
            <div class="layui-input-block" >
                <select name="iden_unitvl_id" id="idenUnitsValueBOX">

                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">指标描述:</label>
            <div class="layui-input-block">
                        <textarea name="memo" placeholder="请输入描述" class="layui-textarea"
                                  lay-verType="tips" lay-verify="" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否展示</label>
            <div class="layui-input-block">
                <input type="checkbox" checked name="is_displayed" lay-skin="switch" lay-text="是|否" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否业务主键</label>
            <div class="layui-input-block">
                <input type="checkbox" checked name="is_pk" lay-skin="switch" lay-text="是|否" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否可用</label>
            <div class="layui-input-block">
                <input type="checkbox" checked name="status" lay-skin="switch" lay-text="是|否" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否设为搜索列</label>
            <div class="layui-input-block">
                <input type="checkbox" checked name="is_search" lay-skin="switch" lay-text="是|否" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary layui-btn-sm" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn layui-btn-sm" lay-filter="addIdenSubmit" lay-submit >确定</button>
        </div>
    </form>

<!--渲染指标类型-->
<script type="text/tmpl" id="template-idenType-tmpl">
    <select name="iden_type" placeholder="请选择"  lay-filter="idenType">
         {{each data}}
            <option value="{{$value.value}}">{{$value.iden_type}}</option>
        {{/each}}
    </select>
</script>
<!--渲染指标获取渠道-->
<script type="text/tmpl" id="template-procurementType-tmpl">
    <select name="data_procurement" placeholder="请选择">
         {{each data}}
            <option value="{{$value.value}}">{{$value.procurement_type}}</option>
        {{/each}}
    </select>
</script>
<!--渲染指标使用方式-->
<script type="text/tmpl" id="template-userType-tmpl">
    <select name="data_use" placeholder="请选择">
         {{each data}}
            <option value="{{$value.value}}">{{$value.use_type}}</option>
        {{/each}}
    </select>
</script>
<!--渲染指标单位分组-->
<script type="text/tmpl" id="template-idenUnitsGroup-tmpl">

         {{each records}}
            <option value="{{$value.id}}">{{$value.cnname}}</option>
        {{/each}}

</script>
<!--渲染指标单位值-->
<script type="text/tmpl" id="template-idenUnitsValue-tmpl">
     {{each records}}
        <option value="{{$value.id}}">{{$value.cnname}}</option>
    {{/each}}
</script>
<!--渲染枚举分组-->
<script type="text/tmpl" id="template-microGroup-tmpl">
    <option value="">请选择</option>
     {{each records}}
        <option value="{{$value.id}}">{{$value.name}}</option>
    {{/each}}

</script>
<!--渲染枚举分组版本-->
<script type="text/tmpl" id="template-microValue-tmpl">
    <option value="">请选择</option>
     {{each data}}
        <option value="{{$value.id}}">{{$value.value}}</option>
    {{/each}}
</script>
<script>
    layui.use(['layer', 'form', 'admin','tree','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var tree = layui.tree;
        var laydate = layui.laydate;
        var idenCofig = {//指标管理配置
            count:0,//总个数
            current:1,//当前页
            idenName:'',//指标名称
            fistRenderPage:true,//指标是否第一次渲染分页
            iden_type:'',//指标类型

            idenType : {},//指标类型
            procurementType:{},//获取渠道
            userType:{},//指标使用方式
            idenUnitsGroup:{},//单位分组
            microGroup:{},//枚举分组
        }

        var publicData = window.publicData;  // 列表页面传递的数据，也可以使用top.xxx
        var Authorization;
        var userInfo = getAuthority();
        //获取权限方法
        if(userInfo){
            var access_token = localStorage.getExpire("access_token");
            var token_type = localStorage.getExpire("token_type");
            Authorization = token_type+" "+access_token;
            getIdenOtherInfo();
        }
        function onload() {
            var strObj = initOtherType();
            document.getElementById("addIdenTypeBox").innerHTML = strObj.idenType;
            document.getElementById("procurementTypeBox").innerHTML = strObj.procurementType;
            document.getElementById("useTypeBox").innerHTML = strObj.userType;
            document.getElementById("idenUnitsGroupBOX").innerHTML = strObj.idenUnitsGroup;
            document.getElementById("identGroupBOX").innerHTML = strObj.microGroup;
            $(".iden_precision,.identGroup,.ident_version,.idenUnitsGroup,.idenUnitsValue").hide();//默认数据类型为文本
            form.render('select');
            form.render('checkbox');
            form.on('select(idenType)', function(data){
                if(data.value!=""){
                    setAddIdentModelShowAndHidden(data.value);
                }
            });
            form.on('select(idenUnitsGroupBOX)', function(data){
                if(data.value!=""){
                    getIdenUnitSValue(data.value,function (str) {
                        document.getElementById("idenUnitsValueBOX").innerHTML = str;
                        form.render('select')
                    });
                }
            });
            form.on('select(identGroup)', function(data){
                if(data.value!=""){
                    getIdentGroupVersion(data.value,function(str){
                        document.getElementById("ident_version_box").innerHTML = str;
                        form.render('select')
                    });
                }
            });
            // 表单提交事件
            form.on('submit(addIdenSubmit)', function (data) {
                switch (Number(data.field.iden_type)){
                    case 1:
                        if(data.field.iden_length==""){
                            layer.msg('请设置完整参数', {icon: 2});
                            return false;
                        }
                        break;
                    case 2:
                        if(data.field.iden_length==""||data.field.iden_unit_name==""||data.field.iden_unitvl_id==""){
                            layer.msg('请设置完整参数', {icon: 2});
                            return false;
                        }
                        break;
                    case 4:
                        if(data.field.identid==""||data.field.ident_version==""){
                            layer.msg('请设置完整参数', {icon: 2});
                            return false;
                        }
                        break;
                    default:
                        break;
                }

                var formData = {
                    'data_procurement':data.field.data_procurement,
                    'data_use':data.field.data_use,
                    'iden_alias_code':data.field.iden_alias_code||'',
                    'iden_name':data.field.iden_name,
                    'iden_precision':data.field.iden_precision,
                    'iden_simple_name':data.field.iden_simple_name,
                    'iden_type':data.field.iden_type,
                    'memo':data.field.memo,
                    'mitid':publicData.id,
                    'sort':0,
                    'is_displayed':data.field.hasOwnProperty('is_displayed')?1:0,
                    'is_pk':data.field.hasOwnProperty('is_pk')?1:0,
                    'is_search':data.field.hasOwnProperty('is_search')?1:0,
                    'status':data.field.hasOwnProperty('status')?1:0
                };

                if(data.field.iden_type == 1){//文本
                    formData.iden_length = data.field.iden_length
                }else if(data.field.iden_type == 2){//数字
                    formData.iden_length = data.field.iden_length
                    formData.iden_unit_name = data.field.iden_unit_name
                    formData.iden_unitvl_id = data.field.iden_unitvl_id
                }else if(data.field.iden_type == 3){//日期
                    formData.iden_length = 255
                }else if(data.field.iden_type == 4){//枚举
                    formData.ident_version = data.field.ident_version
                    formData.identid = data.field.identid
                    formData.iden_length = 255
                }
                axios({
                    method: 'POST',
                    url:microUrl.iden.addIden,
                    data:$.param(formData),
                    headers:{
                        'Authorization':Authorization
                    }
                })
                    .then(function (response) {
                        if(response.status == 200 &&response.data.message == "success"){
                            layer.msg("新增成功", {icon: 1});
                        }else{
                            layer.msg(response.data.message, {icon: 2});
                        }
                        layer.closeAll('loading');
                        admin.closeDialog('#addIdenManageForm');
                    })
                    .catch(function (error) {
                        console.log(error);
                        if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                            // 进行重新登陆
                            localStorage.clear();
                            top.location.href=loginPageUrl;
                        }
                    });
                return false;
            });
        }

        /*初始化指标用到的片段*/
        function initOtherType(){
            var strObj = {};
            strObj.idenType = template("template-idenType-tmpl",idenCofig.idenType);
            strObj.procurementType = template("template-procurementType-tmpl",idenCofig.procurementType);
            strObj.userType = template("template-userType-tmpl",idenCofig.userType);
            strObj.idenUnitsGroup = template("template-idenUnitsGroup-tmpl",idenCofig.idenUnitsGroup);
            strObj.microGroup = template("template-microGroup-tmpl",idenCofig.microGroup);

            return strObj;
        }
        /*设置新建指标显示隐藏项*/
        function setAddIdentModelShowAndHidden(type) {
            $(".iden_length,.iden_precision,.identGroup,.ident_version,.idenUnitsGroup,.idenUnitsValue").show();
            switch (Number(type)){
                case 1://文本
                    $(".iden_precision,.identGroup,.ident_version,.idenUnitsGroup,.idenUnitsValue").hide();
                    break;
                case 2://数字
                    $(".identGroup,.ident_version").hide();
                    break;
                case 3://日期
                    $(".iden_length,.iden_precision,.identGroup,.ident_version,.idenUnitsGroup,.idenUnitsValue").hide();
                    $("#iden_length input").val(255);
                    break;
                case 4://枚举
                    $(".iden_length,.iden_precision,.idenUnitsGroup,.idenUnitsValue").hide();
                    $("#iden_length input").val(255);
                    break;
                default:
                    break;
            }
        }

        function getIdenUnitSValue(group,callback) {
            var req={
                current : 1,
                size: 1000,
                parid:group
            };
            axios.get(metadataUrl.unitsGroup.idenData.idenDataList,{
                params: req,
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        var str = template("template-idenUnitsValue-tmpl",data);
                        callback(str);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }

        function getIdentGroupVersion(gid,callback) {
            axios.get(microUrl.version.queryVersion+gid,{
                params: {},
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data;
                        var str = template("template-microValue-tmpl",data);
                        callback(str);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        /*获取指标类型、获取渠道、使用方式*/
        function getIdenOtherInfo(){
            function getIdenType() {
                return axios.get(microUrl.iden.idenType,{
                    params: {},
                    headers:{
                        'Authorization':Authorization
                    }
                })
            }
            function getProcurementType() {
                return axios.get(microUrl.iden.procurementType,{
                    params: {},
                    headers:{
                        'Authorization':Authorization
                    }
                })
            }
            function getUserType() {
                return axios.get(microUrl.iden.userType,{
                    params: {},
                    headers:{
                        'Authorization':Authorization
                    }
                })
            }
            //指标单位分组
            function getIdenUnitsGroup() {
                var req={
                    current:1,
                    size:1000
                };
                return axios.get(metadataUrl.unitsGroup.allUnitsGroup.unitsGroupList,{
                    params: req,
                    headers:{
                        'Authorization':Authorization
                    }
                })
            }
            //枚举分组
            function getMicroGroup() {
                var req={
                    current:1,
                    size:1000
                };
                return axios.get(microUrl.group.queryList,{
                    params: req,
                    headers:{
                        'Authorization':Authorization
                    }
                })
            }
            axios.all([getIdenType(), getProcurementType(),getUserType(),getIdenUnitsGroup(),getMicroGroup()])
                .then(axios.spread(function (idenType, procurementType,userType,idenUnitsGroup,microGroup) {
                    // 两个请求现在都执行完成
                    if(idenType.status == 200 &&idenType.data.message == "success") {
                        var data = idenType.data;
                        idenCofig.idenType = data;
                    }
                    if(procurementType.status == 200 &&procurementType.data.message == "success"){
                        var procurementTypeData = procurementType.data;
                        idenCofig.procurementType = procurementTypeData;
                    }
                    if(userType.status == 200 &&userType.data.message == "success"){
                        var userType = userType.data;
                        idenCofig.userType = userType;
                    }
                    if(idenUnitsGroup.status == 200 &&idenUnitsGroup.data.message == "success"){
                        var idenUnitsGroup = idenUnitsGroup.data.data;
                        idenCofig.idenUnitsGroup = idenUnitsGroup;
                    }
                    if(microGroup.status == 200 &&microGroup.data.message == "success"){
                        var microGroup = microGroup.data.data;
                        idenCofig.microGroup = microGroup;
                    }

                }))
                .then(function (response) {
                    onload()
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
    });
</script>